<?php
/*
 * Created on
 * Date: Feb 15, 2008
 * Author: William Yang
 */
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");

class SearchEngine {
	private $search_by;
	private $pagibar_html;
	
	const SEARCHBY_USER		= "user";
	const SEARCHBY_TEAM		= "team";
	const SEARCHBY_MATCH	= "match";
	
	public function __construct($search_by) {
		$this->pagibar_html = null;
		
		if ($search_by == SearchEngine::SEARCHBY_USER ||
			$search_by == SearchEngine::SEARCHBY_TEAM ||
			$search_by == SearchEngine::SEARCHBY_MATCH)
			$this->search_by = $search_by;
		else
			$this->search_by = NULL;
	}
	
	public function getPagiBarHtml() {
		return $this->pagibar_html;
	}
	
	/*
	 * Note, $count == -1, counting is required.
	 */
	public function search($params, $from, $go2page, $rows_per_page, $get_params, $page_count = -1) {
		if ($this->search_by == NULL)
			return array();
			
		if ($this->search_by == SearchEngine::SEARCHBY_USER) {
			return $this->_search_user($params, $from, $go2page, $rows_per_page, $get_params, $page_count);
		} else if ($this->search_by == SearchEngine::SEARCHBY_TEAM) {
			return $this->_search_team($params, $from, $go2page, $rows_per_page, $get_params, $page_count);
		} else if ($this->search_by == SearchEngine::SEARCHBY_MATCH) {
			return $this->_search_match($params, $from, $go2page, $rows_per_page, $get_params, $page_count);
		} else {
			return array();
		}
	}
	
	/*
	 * $params keys:
	 * NAME
	 * AGEFROM
	 * AGETO
	 * COUNTRY
	 * CITY
	 * GENDER
	 * 
	 * Return value:
	 * The array of the User objects queried.
	 */
	private function _search_user($params, $from, $go2page, $rows_per_page, $get_params, $page_count = -1) {
		$val_name		= NULL;
		$val_age_from	= NULL;
		$val_age_to		= NULL;
		$val_province	= NULL;
		$val_city		= NULL;
		$val_gender		= NULL;
		
		if (isset ($params["NAME"]))
			$val_name = $params["NAME"];
		if (isset ($params["AGEFROM"]))
			$val_age_from = $params["AGEFROM"];
		if (isset ($params["AGETO"]))
			$val_age_to = $params["AGETO"];
		if (isset ($params["PROVINCE"]))
			$val_province = $params["PROVINCE"];
		if (isset ($params["CITY"]))
			$val_city = $params["CITY"];
		if (isset ($params["GENDER"]))
			$val_gender = $params["GENDER"];
		
		$cond_list = new SqlCondList();
		if ($val_age_from != NULL)
			$cond_list->add_condition(new SqlCondition("age", SqlCondition::NOLESSTHAN, $val_age_from));
		
		if ($val_age_to != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("age", SqlCondition::NOLARGERTHAN, $val_age_to));
		}	
		if ($val_province != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("province", SqlCondition::LIKE, "%".$val_province."%"));
		}
		if ($val_city != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("city", SqlCondition::LIKE, "%".$val_city."%"));
		}
		if ($val_gender != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("gender", SqlCondition::EQUAL, $val_gender));
		}
		if ($val_name != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
				
			$names = split("\|", $val_name);
			$cnt = count($names);
			if ($cnt > 1)
				$cond_list->add_start_bracket();
			
			$i = 0;
			for(; $i < $cnt - 1; $i++) {
				$tmp = trim($names[$i]);
				if ($tmp != "") {
					$cond_list->add_condition(new SqlCondition("name", SqlCondition::LIKE, "%".$tmp."%"));
					$cond_list->add_relation(SqlCondition::_OR);
				}
			}
			$tmp = trim($names[$i]);
			if ($tmp != "")
				$cond_list->add_condition(new SqlCondition("name", SqlCondition::LIKE, "%".$tmp."%"));
			
			if ($cnt > 1)
				$cond_list->add_end_bracket();
		}
		
		$cond_list->add_constraint(new SqlConstraint(SqlConstraint::ORDERBY, "uid"));
		$cond_list->add_sort(new SqlSort(SqlSort::DESC));
		$cond_list->add_limit(new SqlLimit($rows_per_page));
		
		/*
		 * Get user objects from database.
		 */	
		$op = new PagiUserManager();
		$op->config(CONFIGURATION_DIR_PATH."/user_table.xml");
		if ($page_count != -1)
			$op->setCountOfPage($page_count);
		
		$op->setCondition($cond_list);
		$op->go2page($go2page, $rows_per_page, true);
		$this->pagibar_html = $op->getHtml($from, $get_params);
		
		$user_objs = $op->getResultSet();
		
		/*$db_opera = new DatabaseOperator();
		$db_opera->config(CONFIGURATION_DIR_PATH."/user_table.xml");
		$ret_array = $db_opera->select($cond_list);
		
		if ($ret_array === NULL) {
			return NULL;
		} else {
			$ret = array();
			$user_delegate = new User();
			foreach ($ret_array as $row) {
				$tmp = $user_delegate->array2copy($row);
				$ret[] = $tmp;
			}
			return $ret;
		}*/
		
		return $user_objs;
	}
	
	/*
	 * $params keys:
	 * NAME
	 * COUNTOFMATCHESFROM
	 * COUNTOFMATCHESTO
	 * WINPERCENTAGEFROM
	 * WINPERCENTAGETO
	 * COUNTRY
	 * CITY
	 * 
	 * Return value:
	 * The array of the Team objects queried.
	 */
	private function _search_team($params, $from, $go2page, $rows_per_page, $get_params, $page_count = -1) {
		$val_name			= NULL;
		$val_cnt_match_from	= NULL;
		$val_cnt_match_to	= NULL;
		$val_win_pct_from	= NULL;
		$val_win_pct_to		= NULL;
		$val_province		= NULL;
		$val_city			= NULL;
		
		if (isset ($params["COUNTOFMATCHESFROM"]))
			$val_cnt_match_from = $params["COUNTOFMATCHESFROM"];
		if (isset ($params["COUNTOFMATCHESTO"]))
			$val_cnt_match_to = $params["COUNTOFMATCHESTO"];
		if (isset ($params["WINPERCENTAGEFROM"]))
			$val_win_pct_from = $params["WINPERCENTAGEFROM"];
		if (isset ($params["WINPERCENTAGETO"]))
			$val_win_pct_to = $params["WINPERCENTAGETO"];
		if (isset ($params["PROVINCE"]))
			$val_province = $params["PROVINCE"];
		if (isset ($params["CITY"]))
			$val_city = $params["CITY"];
		if (isset ($params["NAME"]))
			$val_name = $params["NAME"];
			
		$cond_list = new SqlCondList();
		if ($val_cnt_match_from != NULL)
			$cond_list->add_condition(new SqlCondition("cntofmatch", SqlCondition::NOLESSTHAN, $val_cnt_match_from));
		if ($val_cnt_match_to != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("cntofmatch", SqlCondition::NOLARGERTHAN, $val_cnt_match_to));
		}
		if ($val_win_pct_from != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("win_pct", SqlCondition::NOLESSTHAN, $val_win_pct_from));
		}
		if ($val_win_pct_to != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("win_pct", SqlCondition::NOLARGERTHAN, $val_win_pct_to));
		}
		if ($val_province != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("province", SqlCondition::LIKE, "%".$val_province."%"));
		}
		if ($val_city != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("city", SqlCondition::LIKE, "%".$val_city."%"));
		}
		if ($val_name != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
				
			$names = split("\|", $val_name);
			$cnt = count($names);
			if ($cnt > 1)
				$cond_list->add_start_bracket();
			
			$i = 0;
			for(; $i < $cnt - 1; $i++) {
				$tmp = trim($names[$i]);
				if ($tmp != "") {
					$cond_list->add_condition(new SqlCondition("name", SqlCondition::LIKE, "%".$tmp."%"));
					$cond_list->add_relation(SqlCondition::_OR);
				}
			}
			$tmp = trim($names[$i]);
			if ($tmp != "")
				$cond_list->add_condition(new SqlCondition("name", SqlCondition::LIKE, "%".$tmp."%"));
			
			if ($cnt > 1)
				$cond_list->add_end_bracket();
		}
		$cond_list->add_constraint(new SqlConstraint(SqlConstraint::ORDERBY, "uid"));
		$cond_list->add_sort(new SqlSort(SqlSort::DESC));
		$cond_list->add_limit(new SqlLimit($rows_per_page));
		
		/*
		 * Get team objects from database.
		 */	
		$op = new PagiTeamManager();
		$op->config(CONFIGURATION_DIR_PATH."/team_table.xml");
		if ($page_count != -1)
			$op->setCountOfPage($page_count);
		
		$op->setCondition($cond_list);
		$op->go2page($go2page, $rows_per_page, true);
		$this->pagibar_html = $op->getHtml($from, $get_params);
		
		$team_objs = $op->getResultSet();
		
		return $team_objs;
	}
	
	/*
	 * $params keys:
	 * NAME
	 * PLAYDATEFROM
	 * PLAYDATETO
	 * COUNTRY
	 * CITY
	 * HOTFROM
	 * HOTTO
	 * HOMETEAMNAME
	 * 
	 * Return value:
	 * The array of the Match objects queried.
	 */
	private function _search_match($params, $from, $go2page, $rows_per_page, $get_params, $page_count = -1) {
		$val_name			= NULL;
		$val_play_date_from	= NULL;
		$val_play_date_to	= NULL;
		$val_hot_from		= NULL;
		$val_hot_to			= NULL;
		$val_province		= NULL;
		$val_city			= NULL;
		$val_home_team_name	= NULL;
		
		if (isset ($params["PLAYDATEFROM"]))
			$val_play_date_from = $params["PLAYDATEFROM"];
		if (isset ($params["PLAYDATETO"]))
			$val_play_date_to = $params["PLAYDATETO"];
		if (isset ($params["HOTFROM"]))
			$val_hot_from = $params["HOTFROM"];
		if (isset ($params["HOTTO"]))
			$val_hot_to = $params["HOTTO"];
		if (isset ($params["PROVINCE"]))
			$val_province = $params["PROVINCE"];
		if (isset ($params["CITY"]))
			$val_city = $params["CITY"];
		if (isset ($params["NAME"]))
			$val_name = $params["NAME"];
		if (isset ($params["HOMETEAMNAME"]))
			$val_home_team_name = $params["HOMETEAMNAME"];
			
		$cond_list = new SqlCondList();
		if ($val_play_date_from != NULL) {
			$cond_list->add_condition(new SqlCondition("playdate", SqlCondition::NOLESSTHAN, $val_play_date_from));
		}
		if ($val_play_date_to != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("playdate", SqlCondition::NOLARGERTHAN, $val_play_date_to));
		}
		if ($val_hot_from != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("hot_index", SqlCondition::NOLESSTHAN, $val_hot_from));
		}
		if ($val_hot_to != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("hot_index", SqlCondition::NOLARGERTHAN, $val_hot_to));
		}
		if ($val_province != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("province", SqlCondition::LIKE, "%".$val_province."%"));
		}
		if ($val_city != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("city", SqlCondition::LIKE, "%".$val_city."%"));
		}
		if ($val_name != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
				
			$names = split("\|", $val_name);
			$cnt = count($names);
			if ($cnt > 1)
				$cond_list->add_start_bracket();
			
			$i = 0;
			for(; $i < $cnt - 1; $i++) {
				$tmp = trim($names[$i]);
				if ($tmp != "") {
					$cond_list->add_condition(new SqlCondition("name", SqlCondition::LIKE, "%".$tmp."%"));
					$cond_list->add_relation(SqlCondition::_OR);
				}
			}
			$tmp = trim($names[$i]);
			if ($tmp != "")
				$cond_list->add_condition(new SqlCondition("name", SqlCondition::LIKE, "%".$tmp."%"));
			
			if ($cnt > 1)
				$cond_list->add_end_bracket();
		}
		if ($val_home_team_name != NULL) {
			if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
			$cond_list->add_condition(new SqlCondition("owner_name", SqlCondition::LIKE, "%".$val_home_team_name."%"));
		}
		
		// Just query public matches
		if (!$cond_list->is_empty_condition())
				$cond_list->add_relation(SqlCondition::_AND);
		$cond_list->add_condition(new SqlCondition("property", SqlCondition::EQUAL, MatchProperty::MATCH_PUBLIC));
		
		$cond_list->add_constraint(new SqlConstraint(SqlConstraint::ORDERBY, "uid"));
		$cond_list->add_sort(new SqlSort(SqlSort::DESC));
		$cond_list->add_limit(new SqlLimit($rows_per_page));
		
		/*
		 * Get match objects from database.
		 */	
		$op = new PagiMatchManager();
		$op->config(CONFIGURATION_DIR_PATH."/match_table.xml");
		if ($page_count != -1)
			$op->setCountOfPage($page_count);
		
		$op->setCondition($cond_list);
		$op->go2page($go2page, $rows_per_page, true);
		$this->pagibar_html = $op->getHtml($from, $get_params);
		
		$match_objs = $op->getResultSet();
		
		return $match_objs;
	}
}
?>
